
package com.anjiplus.template.gaea.business.modules.https.controller;

import com.anji.plus.gaea.annotation.Permission;
import com.anji.plus.gaea.annotation.log.GaeaAuditLog;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.controller.GaeaBaseController;
import com.anji.plus.gaea.curd.service.GaeaBaseService;
import com.anji.plus.gaea.holder.UserContentHolder;
import com.anji.plus.gaea.utils.GaeaUtils;
import com.anjiplus.template.gaea.business.modules.https.controller.dto.HttpDto;
import com.anjiplus.template.gaea.business.modules.https.controller.param.HttpParam;
import com.anjiplus.template.gaea.business.modules.https.dao.entity.Http;
import com.anjiplus.template.gaea.business.modules.https.service.HttpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Raod
 * @desc 数据集 controller
 * @website https://gitee.com/anji-plus/gaea
 * @date 2021-03-18 12:11:31.150755900
 **/
@RestController
@RequestMapping("/http")
public class HttpController extends GaeaBaseController<HttpParam, Http, HttpDto> {

    @Autowired
    private HttpService httpService;

    @Override
    public GaeaBaseService<HttpParam, Http> getService() {
        return httpService;
    }

    @Override
    public Http getEntity() {
        return new Http();
    }

    @Override
    public HttpDto getDTO() {
        return new HttpDto();
    }

    @GetMapping("/detailBysetId/{id}")
    @Permission(code = "query", name = "明细")
    public ResponseBean detailBysetId(@PathVariable("id") String id) {
        this.logger.info("{}根据ID查询服务开始，id为：{}", this.getClass().getSimpleName(), id);
        ResponseBean responseBean = this.responseSuccessWithData(httpService.detailSetById(id));
        this.logger.info("{}根据ID查询结束，结果：{}", this.getClass().getSimpleName(), GaeaUtils.toJSONString(responseBean));
        return responseBean;
    }


    @PostMapping
    @Permission(
            code = "insert",
            name = "新增"
    )
    @GaeaAuditLog(
            pageTitle = "新增"
    )
    @Override
    public ResponseBean insert(@RequestBody HttpDto dto) {
        this.logger.info("{}新增服务开始，参数：{}", this.getClass().getSimpleName(), GaeaUtils.toJSONString(dto));
        HttpDto HttpDto = httpService.insertSet(dto);
        this.logger.info("{}新增服务结束，结果：{}", this.getClass().getSimpleName(), GaeaUtils.toJSONString(HttpDto));
        return ResponseBean.builder().data(HttpDto).build();
    }

    @PutMapping
    @Permission(
            code = "update",
            name = "更新"
    )
    @GaeaAuditLog(
            pageTitle = "修改"
    )
    @Override
    public ResponseBean update(@RequestBody HttpDto dto) {
        String username = UserContentHolder.getContext().getUsername();
        this.logger.info("{}更新服务开始,更新人：{}，参数：{}", this.getClass().getSimpleName(), username, GaeaUtils.toJSONString(dto));
        ResponseBean responseBean = this.responseSuccess();
        httpService.updateSet(dto);
        this.logger.info("{}更新服务结束，结果：{}", this.getClass().getSimpleName(), GaeaUtils.toJSONString(responseBean));
        return this.responseSuccess();
    }

    @DeleteMapping({"/{id}"})
    @Permission(
            code = "delete",
            name = "删除"
    )
    @GaeaAuditLog(
            pageTitle = "删除"
    )
    @Override
    public ResponseBean deleteById(@PathVariable("id") String id) {
        this.logger.info("{}删除服务开始，参数ID：{}", this.getClass().getSimpleName(), id);
        httpService.deleteSet(id);
        this.logger.info("{}删除服务结束", this.getClass().getSimpleName());
        return this.responseSuccess();
    }


    /**
     * 获取所有数据集 根据文件存档
     *
     * @return
     */
    @GetMapping("/queryAllDataSet/{dirId}")
    public ResponseBean queryAllDataSet(@PathVariable String dirId) {
        return responseSuccessWithData(httpService.queryAllDataSet(dirId));
    }


    @PostMapping("/copy")
    @Permission(code = "copy", name = "复制")
    @GaeaAuditLog(pageTitle = "复制")
    public ResponseBean copy(@RequestBody HttpDto dto) {
        httpService.copy(dto);
        return ResponseBean.builder().build();
    }

    /**
     * 测试发送http请求
     */
    @PostMapping("/testRequest")
    public Object testRequest(@RequestBody HttpDto dto, HttpServletResponse response) {


        return httpService.sendRequest(dto,response);
    }

    @RequestMapping("/testResult")
    public Object testResult() {

        return "{\"code\":\"success\",\"msg\":\"success\"}";
    }

    /**
     * 查询所有配置了定时的任务
     *
     * @return com.anji.plus.gaea.bean.ResponseBean
     * @author WanShen
     * @date 2025/3/27 下午5:34
     */
    @GetMapping("/queryAllQuartz")
    public ResponseBean queryAllQuartz() {

        return responseSuccessWithData(httpService.queryAllQuartz());
    }
}
